﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;

using DevExpress.XtraTabbedMdi;
using BUS;
using QLDT.Report;
using DTO;
using DevExpress.XtraEditors;
namespace QLDT
{
    public partial class frm_main : RibbonForm
    {
        DTO.DTO_TaiKhoan taikhoan;
        List<DTO_HeThong> listPhanQuyen;
        public frm_main()
        {
            InitializeComponent();
            InitSkin();
        }
        public frm_main(DTO.DTO_TaiKhoan tk)
        {
            taikhoan = new DTO.DTO_TaiKhoan();
            taikhoan = tk;
            InitializeComponent();
            InitSkin();
        }
        public void InitSkin()
        {
            SkinHelper.InitSkinGallery(bar_GD, true);
            XtraTabbedMdiManager mdiManager = new XtraTabbedMdiManager();
            mdiManager.MdiParent = this;
        }
        private void LoadTenNguoiDung()
        {
            if (taikhoan.ID_Nhom == 3)
            {
                taikhoan.TenNguoiDung = BUS_SinhVien.SelectSinhVienTheoMaSV(taikhoan.TenTK)[0].TenSV;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadTenNguoiDung();
            LoadThongTin();
            PhanQuyen();


        }
        public void addformMain(System.Windows.Forms.Form frm)
        {
            foreach (System.Windows.Forms.Form _mdi in this.MdiChildren)
            {
                if (_mdi.Name == frm.Name)
                    _mdi.Close();
            }
            frm.MdiParent = this;
            frm.Show();
        }
        private void barBt_ChuyenNganh_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_ChuyenNganh frm = new frm_ChuyenNganh();
            addformMain(frm);
        }

        private void barBt_QLDanhsachtai_ItemClick(object sender, ItemClickEventArgs e)
        {
            QLPhanCongDT frm = new QLPhanCongDT();
            addformMain(frm);
        }

        private void barBt_DMKhoa_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_DMKhoa frm = new frm_DMKhoa();
            addformMain(frm);
        }
        private void barBt_DMKhoahoc_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_DMKhoaHoc frm = new frm_DMKhoaHoc();
            addformMain(frm);
        }
        private void barBt_DMLop_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_DMLop frm = new frm_DMLop();
            addformMain(frm);
        }

        private void barBt_QLGuidetai_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmQLDeTai frm = new frmQLDeTai(taikhoan);
            addformMain(frm);
        }

        private void barBt_QLDanhsachgiangvien_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_DSGiangVien frm = new frm_DSGiangVien();
            addformMain(frm);
        }

        private void barBt_QLDuyetdetai_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDuyetDeTai frm = new frmDuyetDeTai();
            addformMain(frm);
        }

        private void barBt_taoPhancongDT_ItemClick(object sender, ItemClickEventArgs e)
        {
            NhapDiem frm = new NhapDiem(taikhoan);
            addformMain(frm);
        }

        private void btnChonDT_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_ChonDeTai frm = new frm_ChonDeTai(taikhoan);
            addformMain(frm);
        }

        private void barBt_QLDanhsachsinhvien_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_DSSinhVien frm = new frm_DSSinhVien();
            addformMain(frm);
        }

        private void barBt_HTSaoLuu_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frm_Backup frm = new Frm_Backup();
            frm.Show();
        }

        private void barBt_HTQuanlytaikhoan_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frm_QLTaiKhoan frm = new Frm_QLTaiKhoan();
            addformMain(frm);
        }

        private void barBt_HTPhuchoi_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_Retore frm = new frm_Retore();
            frm.Show();
        }

        private void barBt_BCSinhviendau_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_BaoCaoSVDau frm = new frm_BaoCaoSVDau();
            addformMain(frm);
        }

        private void barBt_BCdanhsachdetai_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_report_DeTai frm = new frm_report_DeTai();
            addformMain(frm);
        }

        private void barbt_DanhSachSV_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_report_BaoCaoSinhVien frm = new frm_report_BaoCaoSinhVien();
            addformMain(frm);
        }

        private void barBt_Giangvien_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_report_GV frm = new frm_report_GV();
            addformMain(frm);
        }

        private void barBt_BCDanhsachsinhvienrot_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_BaoCaoSVRot frm = new frm_BaoCaoSVRot();
            addformMain(frm);
        }

        private void barBt_QLPhancongdetai_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_QLPhanCongChiTiet_SV frm = new frm_QLPhanCongChiTiet_SV(taikhoan);
            addformMain(frm);
        }

        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_TaophancongDetai frm = new frm_TaophancongDetai();
            frm.Show();
        }

        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_QLPhanCongChiTiet_GV frm = new frm_QLPhanCongChiTiet_GV(taikhoan);
            addformMain(frm);
        }

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_DoiMK frm = new frm_DoiMK();
            addformMain(frm);
        }

        private void a_ItemClick(object sender, ItemClickEventArgs e)
        {
            NhapDSSVBangExecl frm = new NhapDSSVBangExecl();
            addformMain(frm);
        }

        private void barButtonItem9_ItemClick(object sender, ItemClickEventArgs e)
        {
            NhapxuatExcelGiangVien frm = new NhapxuatExcelGiangVien();
            addformMain(frm);
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frm_DMLop frm = new frm_DMLop();
            addformMain(frm);
        }

        private void btCapnhat_Click(object sender, EventArgs e)
        {

        }

        private void txtSDT_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        public bool FindForm(string form)
        {
            for (int i = 0; i < listPhanQuyen.Count; ++i)
            {
                if (form == listPhanQuyen[i].TenChucNang)
                    return true;
            }
            return false;
        }
        public void PhanQuyen()
        {
            listPhanQuyen = new List<DTO_HeThong>();
            listPhanQuyen = BUS_HeThong.SelectChucNang(taikhoan.ID_Nhom);
            if (FindForm(barBt_DMLop.Name))
                barBt_DMLop.Enabled = true;
            if (FindForm(barBt_DMKhoa.Name))
                barBt_DMKhoa.Enabled = true;
            if (FindForm(barBt_DMChuyennganh.Name))
                barBt_DMChuyennganh.Enabled = true;
            if (FindForm(barBt_DMKhoahoc.Name))
                barBt_DMKhoahoc.Enabled = true;

            if (FindForm(barBt_QLDanhsachsinhvien.Name))
                barBt_QLDanhsachsinhvien.Enabled = true;
            if (FindForm(barBt_QLPhancongdetai.Name))
                barBt_QLPhancongdetai.Enabled = true;
            if (FindForm(bar_SVex.Name))
                bar_SVex.Enabled = true;
            if (FindForm(barBt_QLDanhsachgiangvien.Name))
                barBt_QLDanhsachgiangvien.Enabled = true;
            if (FindForm(bar_QLPCGV.Name))
                bar_QLPCGV.Enabled = true;
            if (FindForm(bar_ExelGV.Name))
                bar_ExelGV.Enabled = true;
            if (FindForm(bar_PC.Name))
                bar_PC.Enabled = true;
            if (FindForm(btnChonDT.Name))
                btnChonDT.Enabled = true;
            if (FindForm(barBt_QLDanhsachtai.Name))
                barBt_QLDanhsachtai.Enabled = true;
            if (FindForm(barBt_QLGuidetai.Name))
                barBt_QLGuidetai.Enabled = true;
            if (FindForm(barBt_QLDuyetdetai.Name))
                barBt_QLDuyetdetai.Enabled = true;
            if (FindForm(barBt_taoPhancongDT.Name))
                barBt_taoPhancongDT.Enabled = true;

            if (FindForm(barbt_DanhSachSV.Name))
                barbt_DanhSachSV.Enabled = true;
            if (FindForm(barBt_BCSinhviendau.Name))
                barBt_BCSinhviendau.Enabled = true;
            if (FindForm(barBt_BCDanhsachsinhvienrot.Name))
                barBt_BCDanhsachsinhvienrot.Enabled = true;
            if (FindForm(barBt_Giangvien.Name))
                barBt_Giangvien.Enabled = true;
            if (FindForm(barBt_BCdanhsachdetai.Name))
                barBt_BCdanhsachdetai.Enabled = true;

            if (FindForm(barBt_HTQuanlytaikhoan.Name))
                barBt_HTQuanlytaikhoan.Enabled = true;
            if (FindForm(barBt_QLNhomguoidung.Name))
                barBt_QLNhomguoidung.Enabled = true;
            if (FindForm(barBt_HTSaoLuu.Name))
                barBt_HTSaoLuu.Enabled = true;
            if (FindForm(barBt_HTPhuchoi.Name))
                barBt_HTPhuchoi.Enabled = true;
            if (FindForm(bar_GD.Name))
                bar_GD.Enabled = true;
            if (FindForm(bar_TTCN.Name))
                bar_TTCN.Enabled = true;
        }
        public void LoadThongTin()
        {
            if (taikhoan.ID_Nhom == 3)
            {
                dockThongtin.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible;
                dock_TTQL.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden;
                dock_QT.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden;
                DTO_SinhVien sv;
                sv = BUS_SinhVien.SelectSinhVienTheoMaSV(taikhoan.TenTK)[0];
                txtHoten.Text = sv.TenSV;
                txtHoten.Enabled = false;
                txtMaSV.Text = sv.MaSV;
                txtMaSV.Enabled = false;
                txtMaLop.Text = sv.MaLop;
                txtMaLop.Enabled = false;
                txtNgaySinh.Text = sv.NgaySinh.ToString();
                txtNgaySinh.Enabled = false;
                if (sv.GioiTinh == false)
                {
                    ch_GT.Checked = false;
                }
                else
                {
                    ch_GT.Checked = true;
                }
                ch_GT.Enabled = false;
                txtSDT.Text = sv.SDT.ToString();
                txtDiaChi.Text = sv.DiaChi;
                txtEmail.Text = sv.Email;
            }
            else if (taikhoan.ID_Nhom == 2)
            {

                dockThongtin.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden;
                dock_TTQL.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible;
                dock_QT.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden;
                DTO_GiangVien gv;
                gv = BUS_GiangVien.SelectGV_TheoMaGV(taikhoan.TenTK)[0];
                txtTenGV.Text = gv.MaGV;
                txtTenGV.Enabled = false;
                txt_MaGV.Text = gv.MaGV;
                txt_MaGV.Enabled = false;
                txt_MaCN.Text = gv.MaCN;
                txt_MaCN.Enabled = false;
                txt_MaKhoa.Text = gv.MaKhoa;
                txt_MaKhoa.Enabled = false;
                txt_LVNC.Text = gv.LVCN;
                txt_LVNC.Enabled = false;
                txt_CNBM.Text = gv.CNBoMon;
                txt_CNBM.Enabled = false;
                if (gv.GioiTinh == false)
                {
                    check_GT.Checked = false;
                }
                else
                {
                    check_GT.Checked = true;
                }
                check_GT.Enabled = false;

                txt_Email.Text = gv.Email;
                txt_DienThoai.Text = gv.DienThoai;

            }

            else if (taikhoan.ID_Nhom == 1)
            {

                dockThongtin.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden;
                dock_TTQL.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Hidden;
                dock_QT.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible;
                DTO_NguoiQuanTri qt;
                qt = BUS_NguoiQuanTri.Select_QuanTri_MaQT(taikhoan.TenTK)[0];
                txt_TenQT.Text = qt.TenQT;
                txt_TenQT.Enabled = false;
                txt_MaQT.Text = qt.MaQT;
                txt_MaQT.Enabled = false;
                txt_NgaySinh.Text = qt.NgaySinh.ToString();
                txt_NgaySinh.Enabled = false;
                if (qt.GioiTinh == false)
                {
                    ch_QT.Checked = false;
                }
                else
                {
                    ch_QT.Checked = true;
                }
                ch_QT.Enabled = false;
                txt_DiaChi.Text = qt.DiaChi;
                ttx_EmailQT.Text = qt.Email;
                txt_SDT.Text = qt.SDT;
            }
        }
        private void btCapnhat_Click_1(object sender, EventArgs e)
        {
            DTO_SinhVien SV = new DTO_SinhVien();
            SV.SDT = txtSDT.Text;
            SV.DiaChi = txtDiaChi.Text;
            SV.Email = txtEmail.Text;
            if (!KTDulieu())
            {
                XtraMessageBox.Show("nhap day du nha", "Thong báo");
                return;
            }
            if (BUS_SinhVien.Update_TTTaiKhoan(SV, taikhoan.TenTK))
            {
                load();
                XtraMessageBox.Show("Ban da cap nhat thong tin thanh cong", "ThongBao");
            }
            else
                XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
        }
        public bool KTDulieu()
        {
            if (txtSDT.Text == "")
                return false;
            if (txtDiaChi.Text == "")
                return false;
            if (txtEmail.Text == "")
                return false;
            return true;

        }
        private void load()
        {
            txtDiaChi.Enabled = false;
            txtEmail.Enabled = false;
            txtSDT.Enabled = false;
        }

        private void simpleBt_Cnhat_Click(object sender, EventArgs e)
        {
            DTO_GiangVien gv = new DTO_GiangVien();

            gv.DienThoai = txt_DienThoai.Text;
            gv.Email = txt_Email.Text;
            if (!KTDulieuGV())
            {
                XtraMessageBox.Show("nhap day du nha", "Thong báo");
                return;
            }
            if (BUS_GiangVien.Update_TTGV(gv, taikhoan.TenTK))
            {
                KTDulieuGV();
                XtraMessageBox.Show("Ban da cap nhat thong tin thanh cong", "ThongBao");
            }
            else
                XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
        }
        public bool KTDulieuGV()
        {
            if (txt_DienThoai.Text == "")
                return false;
            if (txt_Email.Text == "")
                return false;
            return true;

        }
        private void loadGV()
        {
            txt_DienThoai.Enabled = false;
            txt_Email.Enabled = false;
        }

        private void barBt_QLNhomguoidung_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm_PhanQuyen frm = new frm_PhanQuyen();
            frm.Show();
        }

        private void navBarControl1_Click(object sender, EventArgs e)
        {

        }

    }
}